package com.jiuyin.dianjing.ui.fragment.base;

import android.util.Pair;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.adapter.TabAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 基本的抽象ViewPager + TabLayout + Fragment
 */
public abstract class BaseTabFragment<T> extends BaseLazyFragment {

    protected final List<Pair<String, Fragment>> mFragments = new ArrayList<>();
    @BindView(R.id.vp_view_container)
    ViewPager mViewPager;
    @BindView(R.id.tabs)
    TabLayout mTabLayout;
    private TabAdapter mTabAdapter;

    @Override
    protected void initData() {

    }

    protected void initTabLayout() {
        mTabAdapter = new TabAdapter(getChildFragmentManager(), mFragments);
        mViewPager.setAdapter(mTabAdapter);
        mViewPager.setOffscreenPageLimit(mFragments.size());
        mTabLayout.setupWithViewPager(mViewPager);
    }

    /**
     * 添加目标Fragments
     */
    protected abstract void addFragments(List<T> list);

    /**
     * 需要通过它作为调度,加载Fragment
     * 动态初始化列表
     *
     * @param list
     */
    protected void addData(List<T> list) {
        addFragments(list);
        initTabLayout();
    }
}
